package com.example.light.web.personal.controller;

import com.example.light.common.exception.GlobalException;
import com.example.light.common.result.CodeMsg;
import com.example.light.common.result.Result;
import com.example.light.web.personal.entity.Message;
import com.example.light.web.personal.service.MessageService;
import com.example.light.web.system.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
/**
 * @author bengang
 * @date 2018/8/31
 * @time 16:31 留言信息
 * @description
 */
@RequestMapping("message")
@Controller
public class MessageController {

    @Autowired
    private MessageService messageService;

    //---------------- 跳转页面 ----------------
    @RequestMapping("toMessagePage/{page}")
    public String toMessagePage(@PathVariable(value = "page") Integer page, User user, Model model
            , @RequestParam(value = "search", required = false) String search, @RequestParam(value = "status", required = false) Integer status) {
        if (user.getUserEmail() == null || user.getUserEmail().equals("")) {
            throw new GlobalException(CodeMsg.PersonalCodeMsg.EMAIL_FAIL);
        }
        messageService.selectMessages(user.getUserEmail(), page, model, status, search);
        if (status == -3) {
            return "personal/mailbox";
        } else if (status == -1) {
            return "personal/mail_draft";
        } else if (status == -2) {
            return "personal/mail_garbage";
        }
        throw new GlobalException(CodeMsg.PersonalCodeMsg.ILLAGEL_MESSAGE_PAGE);
    }


    //---------------- 留言状态标记 ----------------
    @RequestMapping("mark")
    @ResponseBody
    public Result<Boolean> mark(Message msgId) {
        int res = messageService.update(msgId);
        if (res > 0) {
            return Result.success(true);
        }
        return Result.error(CodeMsg.SystemCodeMsg.ERROR);
    }

    @RequestMapping("remove")
    @ResponseBody
    public Result<Boolean> remove(Message message){
        int res  = messageService.del(message);
        if (res > 0){
            return Result.success(true);
        }
        return Result.error(CodeMsg.SystemCodeMsg.ERROR);
    }

    @RequestMapping("detail")
    public String detail(@RequestParam(value = "messageId") Integer msgId, User user, Model model) {
        Message message = messageService.showOneMessage(msgId, user.getUserEmail(), model);
        model.addAttribute("message", message);
        if(message.getMsgStatus() == -1){
            return "personal/mail_edit";
        }
        return "personal/mail_detail";
    }

    @RequestMapping("toSendPage")
    public String toSendMesspage(Model model, User user) {
        if (user.getUserEmail() == null || user.getUserEmail().equals("")) {
            throw new GlobalException(CodeMsg.PersonalCodeMsg.EMAIL_FAIL);
        }
        messageService.showTips(model, user.getUserEmail());
        return "personal/mail_compose";
    }

    //----------------  ----------------
    @RequestMapping("checkEmail")
    @ResponseBody
    public Result<Boolean> checkEmail(String email, User user) {
        Boolean succ = messageService.checkEmail(email, user.getUserEmail());
        if (succ) {
            return Result.success(true);
        }
        return Result.error(CodeMsg.PersonalCodeMsg.CHECK_EMAIL_FAIL);
    }

    @RequestMapping("sendMessage")
    @ResponseBody
    public Result<Boolean> sendMessage(Message message, User user) {
        if (!messageService.checkEmail(message.getMsgReceiverEmail(),user.getUserEmail()) || user.getUserEmail() == null || user.getUserEmail().equals("")) {
            throw new GlobalException(CodeMsg.PersonalCodeMsg.EMAIL_NON_SET);
        }
        boolean succ = messageService.sendMessage(message, user.getUserEmail());
        if (succ) {
            return Result.success();
        }
        return Result.error(CodeMsg.PersonalCodeMsg.SEND_EMAIL_FAIL);
    }



    @RequestMapping("updateDraft")
    @ResponseBody
    public Result<Boolean> updateDraft(Message message, User user){
        if (!messageService.checkEmail(message.getMsgReceiverEmail(),user.getUserEmail()) || user.getUserEmail() == null || user.getUserEmail().equals("")) {
            throw new GlobalException(CodeMsg.PersonalCodeMsg.EMAIL_NON_SET);
        }
        // 设置更新时间
        message.setMsgTime(new Date());
        int res = messageService.update(message);
        if(res > 0){
            return Result.success();
        }
        return Result.error(CodeMsg.PersonalCodeMsg.UPDATE_DRAFT_FAIL);
    }
}
